#include "main.h"

unsigned char keyState = 0;
unsigned char rxd = 0;
static unsigned char powerState = 0;
void pwrOff();
void pwrOn();

void setup()
{
	uartInitial();
	uartSendString("Setup Success\r\n");
	pwrOff();
}

void loop()
{

	keyState = aoiGetKey();

	if (keyState == 0) {
		delay_ms(20);
		if (aoiGetKey() == 0) {
			while (aoiGetKey() == 0)
				;

			if (powerState == 0) {
				pwrOn();
				delay_ms(30);

				aoiLcdInitial();

				uartSendData(0x3f);
			} else {
				//lcd_clear();
				//P10 = 0;
				pwrOff();
				uartSendData(0x3e);
			}
		}
	}

	if (uartGetRxInt()) {
		uartClearRxFlag();
        
		rxd = uartReciveByte();
		switch (rxd) {
        // 清屏 clear 
		case (0x30):
			aoiLcdClear();
			break;

		case (0x31):
			aoiLcdImage1();
			break;

		case (0x32):
			aoiLcdImage2();
			break;

		case (0x33):
			aoiLcdImage3();
			break;

		case (0x34):
			aoiLcdImage4();
			break;

		case (0x35):
			aoiLcdImage5();
			break;

		case (0x36):
			aoiLcdImage6();
			break;

		case (0x37):
			aoiLcdImage7();
			break;

		case (0x38):
			aoiLcdImage8();
			break;

		case (0x39):
            aoiLcdImage9();
			break;

        case (0x3A):
            aoiLcdImageA();
            break;
        case (0x3B):
            aoiLcdImageB();
			break;

        case (0x3C):
			aoiLcdImageC();
            break;

        // 原始流程
        case (0x3D):
            aoiLcdDefault();
			uartSendString("test uart ok\r\n");
			break;
        
        // 结束信号 ending signal, power off, io=0x00
		case (0x3e):
			pwrOff();
			break;

        // 全显 full display
		case (0x3f):
			aoiLcdFull();
			break;

		default:
			uartSendData(0xff);
			break;
		}

        // return signal
		uartSendData(rxd);
	}
}


void main()
{
	setup();

	while (1) {
		loop();
	}
}


void pwrOn()
{
	setVled(1);
	setVdd(1);
	setVee(1);
	powerState = 1;
}

void pwrOff()
{
	setVee(0);
	setVdd(0);
	setVled(0);
	powerState = 0;
}
